using System;
using System.Collections.Specialized;
using System.ComponentModel.Design;
using EnvDTE;
using Microsoft.Practices.Common;
using Microsoft.Practices.RecipeFramework;

namespace PrismFactory.SoftwareFactory.Extensions.ValueProviders.VisualStudio
{
    public class GetProjectFromGuidValueProvider : ValueProvider, IAttributesConfigurable
    {
        private string fromArgument;

        public override bool OnBeforeActions(object currentValue, out object newValue)
        {
            return OnBeginRecipe(currentValue, out newValue);
        }

        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            newValue = currentValue;
            if (newValue == null)
            {
                IDictionaryService dict = GetService<IDictionaryService>();
                object value = dict.GetValue(fromArgument);
                Guid guid = Guid.Empty;
                if (value is string)
                {
                    guid = new Guid(value as String);
                }
                else
                {
                    guid = (Guid)value;
                }
                newValue = Utility.GetProjectFromGuid(GetService<DTE>(), GetService<IServiceProvider>(), guid);
            }
            return newValue != null;
        }

        public void Configure(StringDictionary attributes)
        {
            fromArgument = attributes["FromArgument"];
        }
    }
}